<?php 
error_reporting(E_ALL);
$PageSecurity = 14;
include_once ('includes/session.inc');
set_time_limit(0);

$action = isset($_POST['action']) ? $_POST['action'] : '';

if ($action == 'getPartner') {
    $result = _single('api/partner')->GetAllPartner($_SESSION['UserID'], $_SESSION['Password']);
    
    $data = '{"success":true';
    $data .= ',"data":[';
    $haveData = false;
    
    while ($row = DB_fetch_array($result)) {
        $haveData = true;
        $data .= sprintf('{"partner_id":"%s","partner_number":"%s","partner_name":"%s"},', $row['partner_id'], $row['partner_number'], $row['partner_name']);
    }
    
    if ($haveData) {
        $data = substr($data, 0, strlen($data) - 1);
    }
    
    $data .= '],"count":"'.json_encode($total).'"}';
    echo $data;
} elseif ($_GET['action'] == 'export') {

    $DEFAULT_EXPORT_FILE_NAME = 'BangTongHopSanLuong';
    $DEFAULT_DIR_UPLOAD = 'tmp/exported/';
    $DEFAULT_EXCEL_TYPE = 'Excel5'; // default Excel 2003 - Excel5,Excel2007
    
    $titleObject = '';
	$titleDaidien = '';
// detail
	// lay du lieu
	$result = _single('api/exchange')->GetSummaryYieldRevenue($_GET['partnerId'], 
	$_GET['fdate'], $_GET['tdate'], &$list_detail, &$list_song, $_SESSION['UserID'], $_SESSION['Password']);
	
    $ObjectDetail = array();
	while ($row = DB_fetch_array($list_detail)) {		
		$ObjectDetail[$row['exchange_imported_date']][$row['song_number']] = $row['exchange_quantity'];
	}
	
	$ObjectResult = array();	
	while ($row = DB_fetch_array($list_song)) {
		$ObjectResult[] = $row;
	}
	
	//print_r($ObjectDetail['04/2011']['6692872']);
	//echo $ObjectDetail['05/2011']['6692872'];
	
    $objPHPExcel = new Api_Excel($DEFAULT_EXPORT_FILE_NAME, $DEFAULT_DIR_UPLOAD, $DEFAULT_EXCEL_TYPE);
    $objPHPExcel->setActiveSheet(0);
	
	$aligment_HorCent_VerCent = array (
		'horizontal'	=> PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
		'vertical' 		=> PHPExcel_Style_Alignment::VERTICAL_CENTER
	);
	
	$aligment_HorCent_VerBottom = array (
		'horizontal'	=> PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
		'vertical' 		=> PHPExcel_Style_Alignment::VERTICAL_BOTTOM
	);
	
    $border_thin = array (
		'style' => PHPExcel_Style_Border :: BORDER_THIN			
	);
    $border_thin_ALL_title = array(
		'borders'=>array(
			'top'	=> $border_thin, 
			'right'	=> $border_thin, 
			'bottom'=> $border_thin, 
			'left'	=> $border_thin
		)
	);
	
	$border_thin_ALL = array(
		'borders'=>array(
			'top'	=> $border_thin, 
			'right'	=> $border_thin, 
			'bottom'=> $border_thin, 
			'left'	=> $border_thin
		),
		'alignment' => $aligment_HorCent_VerBottom,
		'font' => array (
			'bold' 	=> false,
			'size' 	=> 10,
			'color' => array('argb' => 'FFFF0000')
		)
	);
    
	// set default style	
	$objPHPExcel->setPageSetup(85, PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4, PHPExcel_Worksheet_PageSetup::ORIENTATION_PORTRAIT);
	$objPHPExcel->setDefaultStyle('Times New Roman', 12);
	//$objPHPExcel->setFreezePane("E13");
	$objPHPExcel->setPageMargins(0.5, 0.25, 0, 0.75);
	$objPHPExcel->setHorizontalCentered(true);		
		
	$objPHPExcel->setColumnWidth("A", 12)
				->setColumnWidth("B", 35);
	
	$title = '';
	$number_column = 0;
	$start_col = 68;
	$left_col_index = $start_col; // column D
	$right_col_index = 64;
	$list_title_sanluong = array();	
	
	foreach($ObjectDetail as $key => $value){
		$title .= $key.',';
		$number_column++;
		$col_character = getCharacterColumn(&$right_col_index, &$left_col_index);
		$objPHPExcel->mergeCells($col_character . "2", $col_character. "2")
				->setText($col_character . "2", $key, false, false, false, 13)
				->setCellHorizontalAllignment($col_character . "2", 'center')
				->setCellBackgroundColor($col_character . "2", PHPExcel_Style_Fill::FILL_SOLID, "C0C0C0")
				->setDuplicateStyleArray($border_thin_ALL_title, $col_character . "2", $col_character . "2")
				;
				
		$list_title_sanluong[] = $col_character;		
		/*
		 * 04/2011  [029392] [3] D
		 * 			[342342] [6]
		 * 05/2011	[2423423] [6] E
		 * 			[4534534] [3]
		 * 
		 * 
		 */
		for($i = 0; $i < count($ObjectResult); $i++){
			$ObjectResult[$i][$col_character] = $value[$ObjectResult[$i]['song_number']];
		}
	}
	/* NOT FILE NAME
	/*
	if(!empty($title)){
		$title = substr($title, 0, strlen($title) - 1);
	}
	$objPHPExcel->setFileNameExcel($DEFAULT_EXPORT_FILE_NAME . "_[" . $title . "]"); */
	
	$objPHPExcel->mergeCells("A1", "K1")
				->setText("A1","BẢNG TỔNG HỢP SẢN LƯỢNG ĐỐI SOÁT THEO THÁNG, QUÝ [$title]", false, false, false, 14)
				->setCellHorizontalAllignment("A1", 'center')
				->setRowHeight(1, 28)
				
				->setText("A2","Mã số", false, false, false, 13)
				->setCellHorizontalAllignment("A2", 'center')
				->setDuplicateStyleArray($border_thin_ALL_title, "A2", "A2")
				
				->setText("B2","Tên bài hát", false, false, false, 13)
				->setCellHorizontalAllignment("B2", 'center')
				->setDuplicateStyleArray($border_thin_ALL_title, "B2", "B2")
				
				->setText("C2","Tổng", false, false, false, 13)
				->setCellHorizontalAllignment("C2", 'center')
				->setDuplicateStyleArray($border_thin_ALL_title, "C2", "C2")
				->setRowHeight(2, 25)
				->setCellBackgroundColor("A2:C2", PHPExcel_Style_Fill::FILL_SOLID, "C0C0C0")
				
				/*
				->setText("C2","Ca sĩ", false, false, false, 13)
				->setCellHorizontalAllignment("C2", 'center')
				->setDuplicateStyleArray($border_thin_ALL_title, "C2", "C2")
				
				->setText("D2","Nhạc sĩ", false, false, false, 13)
				->setCellHorizontalAllignment("D2", 'center')
				->setDuplicateStyleArray($border_thin_ALL_title, "D2", "D2")
				
				->setText("E2","Tổng", false, false, false, 13)
				->setCellHorizontalAllignment("E2", 'center')
				->setDuplicateStyleArray($border_thin_ALL_title, "E2", "E2")
				->setRowHeight(2, 25)
				->setCellBackgroundColor("A2:E2", PHPExcel_Style_Fill::FILL_SOLID, "C0C0C0")*/
				;
		
	// export detail
	$Position = 3;
	
	//while ($row = DB_fetch_array($list_song)) {
	for($i = 0; $i < count($ObjectResult); $i++){
		$row = $ObjectResult[$i];		
		$objPHPExcel->setText("A".($Position), $row['song_number'])
					->setCellHorizontalAllignment("A".($Position), 'left')
					->setDuplicateStyleArray($border_thin_ALL_title, "A".($Position), "A".$Position)	
					->setText("B".($Position), $row['song_name'])
					->setCellHorizontalAllignment("B".($Position), 'left')
					->setDuplicateStyleArray($border_thin_ALL_title, "B".($Position), "B".($Position))
					
					->setValueExplicit("C".$Position, $row['exchange_quantity'], PHPExcel_Cell_DataType::TYPE_NUMERIC)
					->setFormatCode("C".$Position, $FormatCurrencyExport)
					->setCellHorizontalAllignment("C".$Position, "right")
					->setDuplicateStyleArray($border_thin_ALL_title, "C".($Position), "C".$Position)
					
					->setValueExplicit($col_character.$Position, $val, PHPExcel_Cell_DataType::TYPE_NUMERIC)
					->setFormatCode($col_character.$Position, $FormatCurrencyExport)
					->setCellHorizontalAllignment($col_character.$Position, "right")
					->setDuplicateStyleArray($border_thin_ALL_title, $col_character.$Position, $col_character.$Position)				
						
					/*
					->setText("C".($Position), $row['singer_name'])
					->setCellHorizontalAllignment("C".($Position), 'left')
					->setDuplicateStyleArray($border_thin_ALL_title, "C".($Position), "C".($Position))
					->setText("D".($Position), $row['composer_name'])
					->setCellHorizontalAllignment("D".($Position), 'left')
					->setDuplicateStyleArray($border_thin_ALL_title, "D".($Position), "D".($Position))					
					->setValueExplicit("E".$Position, $row['exchange_quantity'], PHPExcel_Cell_DataType::TYPE_NUMERIC)
					->setFormatCode("E".$Position, $FormatCurrencyExport)
					->setCellHorizontalAllignment("E".$Position, "right")
					->setDuplicateStyleArray($border_thin_ALL_title, "E".($Position), "E".$Position)	*/		
					;
		for($j = 0; $j < count($list_title_sanluong); $j++){
			$col = $list_title_sanluong[$j];			
			$objPHPExcel->setValueExplicit($col.$Position, $row[$col], PHPExcel_Cell_DataType::TYPE_NUMERIC)
						->setFormatCode($col.$Position, $FormatCurrencyExport)
						->setCellHorizontalAllignment($col.$Position, "right")
						->setDuplicateStyleArray($border_thin_ALL_title, $col.$Position, $col.$Position)				
						;
		}			
		/*			
		$left_col_index = $start_col; // column D
		$right_col_index = 64;
		for($i = 0; $i < $number_column; $i++){
			$col_character = getCharacterColumn(&$right_col_index, &$left_col_index);
			
			$val = isset($ObjectDetail[$list_title_sanluong[$col_character."2"]][$row['song_number']]) ? $ObjectDetail[$list_title_sanluong[$col_character."2"]][$row['song_number']] : 0;
			$objPHPExcel->setValueExplicit($col_character.$Position, $val, PHPExcel_Cell_DataType::TYPE_NUMERIC)
						->setFormatCode($col_character.$Position, $FormatCurrencyExport)
						->setCellHorizontalAllignment($col_character.$Position, "right")
						->setDuplicateStyleArray($border_thin_ALL_title, $col_character.$Position, $col_character.$Position)				
						;
			
		}*/		

		$Position++;
	}
		
	$objPHPExcel->outputToBrowser();
	exit;
}

function getCharacterColumn(&$leftcol, &$rightcol){
//	for($i = 0; $i < 55 ; $i++){
//		echo getCharacterColumn(&$right_col_index, &$left_col_index) . " ";
//	}
	$rs = '';
	if($rightcol > 90){
		$leftcol++;
		$rightcol = 65;
	}
	if($leftcol >= 65){
		$rs = chr($leftcol) . chr($rightcol);		
	}else{		
		$rs = chr($rightcol);		
	}
	
	$rightcol++;
	
	return $rs;
}
?>